package com.igd.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;

import com.igd.common.constant.IConstant;
import com.igd.common.constant.SessionConstant;
import com.igd.pojo.vo.ComboxData;
import com.igd.security.User;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.config.entities.InterceptorMapping;

 /**
 * @version  1.0
 *
 * @author   QIAOYU
 *
 * @date     2013-7-2
 *
 * @description	 公共工具包类
 */

public class CommonUtil {
	
	/**
	 * 判断当前action调度中是否存在指定的拦截器
	 * 
	 * @param invocation action调度对象
	 * 
	 * @param interceptorName 拦截器名称
	 * 
	 * @return Boolean
	 */
	public static boolean hasIntercetor(ActionInvocation invocation,String interceptorName){
		
		if(null != invocation && StringUtils.isNotEmpty(interceptorName)){
			
			List<InterceptorMapping> mapping = invocation.getProxy().getConfig().getInterceptors();
			
			if(null != mapping && mapping.size() > 0){
				
				for(InterceptorMapping mapp : mapping){
					
					if(StringUtils.isNotEmpty(interceptorName) && mapp.getName().equals(interceptorName)){

						return Boolean.TRUE;

					}
				}
			}
		}
		
		return Boolean.FALSE;
	}
	
	/**
	 * 根据字段名称生成对应的get方法名
	 * 
	 * @param fieldName
	 * 
	 * @return String
	 */
	public static String createGetMethodName(String fieldName){
		
		String GET_PREFIX = "get";
		
		if(StringUtils.isNotEmpty(fieldName)){
			
			fieldName = GET_PREFIX.concat(fieldName.substring(0,1).toUpperCase().concat(fieldName.substring(1,fieldName.length())));
			
			return fieldName;

		}
		
		return null;
	}
	
	/**
	 * 获取当前登录用户信息
	 * 
	 * @return User
	 */
	public static User getCurrentUser(){
		
		return (User) SecurityUtils.getSubject().getSession().getAttribute(SessionConstant.LOGIN_USER_KEY);
		
	}
	
	/**
	 * null值转换为空字符串,其他正常返回
	 * 
	 * @param val
	 * 
	 * @return String
	 */
	public static String getNullString(Object val){
		
		return null == val ? IConstant.EMPTY_STRING : val.toString();
	}
	
	/**
	 * 判断是否是ajax请求
	 * 
	 * @param request
	 * 
	 * @return boolean
	 */
	public static boolean isAjaxRequest(HttpServletRequest request){
		
		if(null != request){
			
			String X_REQUESTED_WITH = "X-Requested-With";
			
			String AJAX_REQUEST = "XMLHttpRequest";
			
			String ajaxHeader = request.getHeader(X_REQUESTED_WITH);
			
			if(StringUtils.isNotEmpty(ajaxHeader) && !AJAX_REQUEST.equals(ajaxHeader)){
				
				return false;

			}
		}
		
		return true;
	}
	
	/**
	 * 获取ClassPath下文件绝对路径
	 * 
	 * @param fileName
	 * 
	 * @return String
	 */
	public static String getClassPathFile(String fileName){
		
		String classPath = CommonUtil.class.getResource("/").getPath();
		
		if(StringUtils.isNotEmpty(classPath)){
			
			//这里说明获取的是windwos路径
			if(classPath.indexOf(":") > 0){
				
				return classPath.concat(fileName).substring(1);
				
			}else{
				
				return classPath.concat(fileName);
				
			}
		}

		return null;
	}
	
	/**
	 * 根据资源文件所配置的数据可产生Combox数据,例如:集合标签为:user.status.list = 00,99  子数据需配置成user.status.00=正常 user.status.99=冻结
	 * 
	 * @param listKey 集合标签
	 * 
	 * @param valKey 数据标签
	 * 
	 * @return List<ComboxData>
	 */
	public static List<ComboxData> toComboxList(String listKey,String valKey){

		if(StringUtils.isNotEmpty(listKey)){
			
			PropertiesUtil prop = PropertiesUtil.getInstance();
			
			ResourceBundle rb = prop.getResourceBundle("resources");
			
			ComboxData listComBox = prop.getVal(rb, listKey);
			
			if(null != listComBox){
				
				String keys = listComBox.getValue();
				
				if(StringUtils.isNotEmpty(keys)){
					
					String [] key = keys.split(IConstant.SPLIT_KEY);

					if(null != key && key.length > 0){
						
						List<ComboxData> comboxList = new ArrayList<ComboxData>(key.length);
						
						for(String keyStr : key){
							
							ComboxData combox = prop.getVal(rb,valKey.concat(".").concat(keyStr));

							combox.setKey(keyStr);
							
							comboxList.add(combox);

						}
						
						return comboxList;
					}
				}
			}
		}
		
		return null;
	}
}
